/**

 * @author admin

 * 2014年7月10日

 */

package com.tmall.testgen.plugin.views.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.TreeViewer;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.common.bo.interact.back.syn.BaseInfo;
import com.tmall.testgen.plugin.views.GenCodeMonitorView;
import com.tmall.testgen.plugin.views.vo.TreeObject;
import com.tmall.testgen.plugin.views.vo.TreeParent;

/**
 * 按照覆盖未覆盖的方式查看
 * 
* @author nianku haojia.hj@alibaba-inc.com
* @date 2014年7月10日
*
 */
public class CheckInterceptInfoByCoverAction extends Action {
	private TreeParent interceptRoot;
	//private TreeParent mockRoot;
	private TreeViewer viewer;
	
	
	public CheckInterceptInfoByCoverAction() {
		setText("覆盖维度查看");
		setToolTipText("覆盖维度查看");
		setImageDescriptor(ImageManeger.getImageDescriptor(ImageManeger.USED_CLEAR));
	}
	
	public void run() {
		GenCodeMonitorView.ischeckInterceptInfoByTime=false;
		
		List<InterceptInfo> infoes=ClientDataCenter.getInstance().getInfoes();
		if(infoes==null)
			infoes=new ArrayList<InterceptInfo>();
		
		List<InterceptInfo> interceptInfo=new ArrayList<InterceptInfo>();
		List<InterceptInfo> mockInfo=new ArrayList<InterceptInfo>();
		
		for(InterceptInfo info:infoes){
			if(info.getType()==BaseInfo.TYPE_INTERCEPT){
				interceptInfo.add(info);
			}else{
				mockInfo.add(info);
			}
				
		}
		
		interceptRoot.removeAllChildren();
		 Map<String, ArrayList<String>> classAndMethods=  ClientDataCenter.getInstance().getLoadedInterceptClassAndMethods();
		 if(classAndMethods!=null&&classAndMethods.size()>0){
			 addAllToRoot(interceptRoot,classAndMethods,interceptInfo);
		 }
		 
//		 mockRoot.removeAllChildren();
//		 Map<String, ArrayList<String>> mockClassAndMethods=  ClientDataCenter.getInstance().getLoadedMockClassAndMethods();
//		 if(mockClassAndMethods!=null&&mockClassAndMethods.size()>0){
//			 addAllToRoot(mockRoot,mockClassAndMethods,mockInfo);
//		 }
		 
		viewer.refresh();
		
	}
	
	
	public static  void addAllToRoot(TreeParent root, Map<String, ArrayList<String>> classAndMethods,List<InterceptInfo> infoes){
		 Map<String,ArrayList<InterceptInfo>> infoMap=new HashMap<String,ArrayList<InterceptInfo>>();
		 if(infoes!=null){
			 for(InterceptInfo info:infoes){
				 String key=info.getClassName()+"."+info.getMethodName()+info.getMethodParamContent();
				 if(infoMap.get(key)==null){
					 infoMap.put(key, new ArrayList<InterceptInfo>());
				 }
				 infoMap.get(key).add(info);
			 }
		 }
		 
		 
		for(String clazz:classAndMethods.keySet()){
			TreeParent clazzTree = new TreeParent(clazz);
			clazzTree.setLeafNode(false);
			root.addChild(clazzTree);
			
			
			
			for(String method:classAndMethods.get(clazz)){
				String key=clazz+"."+method;
				 ArrayList<InterceptInfo> subInfoes=infoMap.get(key);
				 
				 if(subInfoes==null)
					 subInfoes=new ArrayList<InterceptInfo>();
				 
				TreeParent methodTree = new TreeParent(method+"    ["+subInfoes.size()+"]");
				methodTree.setLeafNode(false);
				clazzTree.addChild(methodTree);
				
				 
				Collections.sort(subInfoes);
				for(InterceptInfo sub:subInfoes){
					TreeParent timeTree = new TreeParent(sub.getExecuteTime());
					timeTree.setLeafNode(false);
					methodTree.addChild(timeTree);
					
					CheckInterceptInfoByTimeAction.addParamAndResultTree(timeTree, sub);
				}
			}
			
			
		}
	}
	/**
	 * 追加
	 */
	public void addInterceptor(InterceptInfo info){
		
		if(info.getType()==BaseInfo.TYPE_INTERCEPT){
			addToRoot(interceptRoot,info);
		}else{
			//addToRoot(mockRoot,info);
		}
		
		viewer.refresh();
		
	}
	
	public static  void addToRoot(TreeParent root,InterceptInfo info ){
		for(TreeObject classTree:root.getChildren()){
			if(classTree.getName().equals(info.getClassName())){
				TreeParent classTreeP=(TreeParent) classTree;
				for(TreeObject methodTree:classTreeP.getChildren() ){
					TreeParent methodTreeP=(TreeParent) methodTree;
					String name=methodTree.getName();
					
					
					int begin=name.indexOf("[");
					int end=name.indexOf("]");
					String content=name.substring(0, begin);
					
					if(!content.trim().equals(info.getMethodName()+info.getMethodParamContent()))
						continue;
					
					
					int times=Integer.valueOf(name.substring(begin+1,end));
					
					methodTree.setName(content+"["+(times+1)+"]");
					
					TreeParent timeTree = new TreeParent(info.getExecuteTime());
					timeTree.setLeafNode(false);
					methodTreeP.addChild(timeTree);
					
					CheckInterceptInfoByTimeAction.addParamAndResultTree(timeTree, info);
					
					
				}
			}
		}
	}



	public TreeViewer getViewer() {
		return viewer;
	}

	public void setViewer(TreeViewer viewer) {
		this.viewer = viewer;
	}

	public TreeParent getInterceptRoot() {
		return interceptRoot;
	}

	public void setInterceptRoot(TreeParent interceptRoot) {
		this.interceptRoot = interceptRoot;
	}


	
	
	
	
	
}
